package com.sat.pay;

import com.github.wxpay.sdk.WXPayUtil;
import com.sat.common.entity.HttpClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@SpringBootTest
@RunWith(value = SpringRunner.class)
public class PayTestApplication {
    /**
     * 微信支付必须满足的要求
     * 1.使用https协议
     * 2.post请求
     * 3.请求参数类型为xml
     */

    @Test
    public void payTest() throws Exception {
        //指定支付的地址
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
        //设置使用https协议
        httpClient.setHttps(true);
        //使用post请求
        httpClient.post();

        //设置xml请求参数
        String xmlParam="<xml><name>lingyi</name></xml>";
        httpClient.setXmlParam(xmlParam);
        //获取支付后返回的数据
        String content = httpClient.getContent();
        System.out.println("获取支付后返回的数据"+content);


    }

    @Test
    public void test() throws Exception {
        HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
        httpClient.setHttps(true);
        httpClient.post();
        //设置参数
        //将map参数转为xml格式的数据
        Map<String,String > map=new HashMap<>();
        //设置公众账号id
        map.put("appid","123456");
        //设置付款金额,以分为单位
        map.put("total_fee","100");
        //设置订单号
        map.put("out_trade_no","1111322346464");

        //使用WXPayUtils工具类将map转为xml格式
        String s = WXPayUtil.mapToXml(map);
        System.out.println(s);

        //将xml数据转为map
        Map<String, String> stringStringMap = WXPayUtil.xmlToMap(s);
        Set<String> strings = stringStringMap.keySet();//获取到map中的所有key
        for (String string : strings) {
            //根据key获取value
            System.out.println("key: "+string+"    value: "+stringStringMap.get(string));
        }
        //生成参数需要的随机字符串
        String s1 = WXPayUtil.generateNonceStr();
        System.out.println(s1);

        WXPayUtil.generateSignature(map,"xxx");
    }
}
